package com.lvkerry.orderfood.service.impl;


import com.lvkerry.orderfood.api.ApiBean;
import com.lvkerry.orderfood.entity.UserEntity;
import com.lvkerry.orderfood.repository.UserRepository;
import com.lvkerry.orderfood.service.PersonService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.lvkerry.orderfood.global.ConstCodeKt.API_ERROR;
import static com.lvkerry.orderfood.global.ConstCodeKt.API_SUCCESS;


/**
 * @author Kerry
 */

@Service
public class PersonServiceImpl implements PersonService {

    private final UserRepository userRepository;

    @Autowired
    public PersonServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }


    @NotNull
    @Override
    public ApiBean addPerson(@NotNull UserEntity person) {

        if (StringUtils.isEmpty(person.getUsername())) {
            return new ApiBean.Builder().setStatus(API_ERROR).setMessage("job_no Can not be empty!").build();
        }
        if (StringUtils.isEmpty(person.getName())) {
            return new ApiBean.Builder().setStatus(API_ERROR).setMessage("name Can not be empty!").build();
        }

        person.setAddTime(System.currentTimeMillis());
        person.setIsDeleted(0);

        UserEntity userEntity = userRepository.findByUsername(person.getUsername());

        if (userEntity != null) {
            return new ApiBean.Builder().setStatus(API_ERROR).setMessage("job no had exits").build();
        } else {
            try {
                userRepository.save(person);
            } catch (Exception e) {
                e.printStackTrace();
                return new ApiBean.Builder().setStatus(API_ERROR).setMessage(e.getMessage()).build();
            }
            return new ApiBean.Builder().setStatus(API_SUCCESS).build();
        }
    }

    @NotNull
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiBean deletePersonByJobNo(@NotNull String jobNo) {
        UserEntity userEntity = userRepository.findByUsername(jobNo);
        if (userEntity != null) {
            //userRepository.delete(userEntity.getId());
            return new ApiBean.Builder().setStatus(API_SUCCESS).build();
        } else {
            return new ApiBean.Builder().setStatus(API_ERROR).setMessage("delete failed").build();
        }
    }

    @NotNull
    @Override
    public ApiBean deletePersonById(@NotNull Long id) {
        try {
            userRepository.delete(id);
            return new ApiBean.Builder().setStatus(API_SUCCESS).build();
        } catch (Exception e) {
            return new ApiBean.Builder().setStatus(API_ERROR).setMessage("delete failed.cause = " + e.getMessage()).build();
        }
    }

    @NotNull
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiBean updatePerson(@NotNull UserEntity person) {
        UserEntity one = null;//= userRepository.findOne(person.getId());
        if (one != null) {
            userRepository.save(person);
            return new ApiBean.Builder().setStatus(API_SUCCESS).build();
        } else {
            return new ApiBean.Builder().setStatus(API_ERROR).setMessage("user id not found").build();
        }
    }

    @NotNull
    @Override
    public ApiBean findAllPerson() {
        List<UserEntity> all = userRepository.findAll();
        return new ApiBean.Builder().setStatus(API_SUCCESS).setData(all).build();
    }
}
